package com.ml.mall.service.country.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.bean.req.country.CountryAddDto;
import com.ml.mall.bean.res.country.CountryQueryAllRes;
import com.ml.mall.entity.country.Country;
import com.ml.mall.enums.CommonEnum;
import com.ml.mall.exception.BizException;
import com.ml.mall.mapper.country.CountryMapper;
import com.ml.mall.service.country.CountryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CountryServiceImpl extends ServiceImpl<CountryMapper, Country> implements CountryService {
    @Autowired
    private CountryMapper countryMapper;

    //实现国家添加操作
    @Override
    public int addCountry(CountryAddDto countryAddDto){
        //检查数据库中是否存在
        QueryWrapper<Country> checkIfHadCountry = new QueryWrapper<>();
        checkIfHadCountry.eq("country_name",countryAddDto.getCountryName());
        Integer dbCountryCount = countryMapper.selectCount(checkIfHadCountry);
        if (dbCountryCount != 0){
            throw new BizException(CommonEnum.ALREADY_EXIST_SAME);
        }
        //执行添加国家操作
        Country insertCountry = new Country();
        insertCountry.setCountryName(countryAddDto.getCountryName());
        int insert = countryMapper.insert(insertCountry);
        if (insert==0){
            throw new BizException(CommonEnum.INSERT_ERROR);
        }
        return insert;
    }

    //实现查询所有国家
    @Override
    public List<CountryQueryAllRes> queryAllCountry(){
        //实现自定义查询所有
        List<CountryQueryAllRes> countryQueryAllRes = countryMapper.queryAllCountry();
        return countryQueryAllRes;
    }
}
